<?php
/* 
 * Copyright (c) 2018-2021   All rights reserved.
 * 
 * 创建时间：2021-10-16 09:35
 *
 * 项目：rm  -  $  - header_htm.php
 *
 * 作者：liwei 
 */

$js = $funcJs = '';
if (!empty($mergeIdens)) {
    foreach ($mergeIdens as $v) {
        $js .= 'jQuery(".Idenv_'.$v.'").parents("tr").find("input[name=\'ids[]\']").attr("checked", true);';
    }
    $funcJs = 'jQuery(".mainIden").val("'.$meInfo['mainiden'].'");';
    $funcJs .= 'jQuery(".checkHideT").val("'.$meInfo['hidelevs'].'");';
    $funcJs .= 'jQuery(".changeAdminEnterInputBox input[name=id]").val("'.$meInfo['adminiden'].'");';
    $funcJs .= 'jQuery(".changeAdminEnterInputBox input[name=r]").val("'.$meInfo['adminroute'].'");';
    $funcJs .= 'jQuery(".changeAdminEnterInputBox input[name=classify]").val("'.$meInfo['adminclassify'].'");';
    $js = 'jQuery(function(){ '.$js.' });';
}
?>

<script><?=$js?></script>

<style>
    .notification.notification-session .item-text.alert-info {min-height: 280px;}
    .column-\#\#btns b svg.icon {font-size: 18px}
</style>

<div class="card">
    <div class="card-header">
        <div class="buttons-row scale8">
            <a class="button button-fill color-lightblue batchZipsBtn">
                <svg class="icon"><use xlink:href="#fa-txb"></use></svg>
                批量压缩
            </a>
            <a class="button button-fill color-bluegray mergeZipsPopupBtn" title="多个模块压缩成一个包，并安装levs模块同时一起安装">
                <svg class="icon"><use xlink:href="#fa-txb"></use></svg>
                合并压缩
            </a>
            <a class="button button-fill color-orange batchZipsUploadPopupBtn">
                <svg class="icon"><use xlink:href="#fa-up"></use></svg>
                批量上传
            </a>
        </div>
        <a class="button-fill button color-orange" target="_blank" href="<?=Lev::toReRoute(['superman/settings', 'classify'=>'servermud', 'iden'=>'levmodules', 'id'=>'levs'])?>">
            <svg class="icon"><use xlink:href="#fa-set"></use></svg>
            服务器配置
        </a>
    </div>
</div>

<div class="hiddenx">
<div class="popup batchZipsUploadPopup">
    <div class="navbar navbar-bgcolor-red">
        <div class="navbar-inner">
            <div class="left"></div>
            <div class="title">服务器上传</div>
            <div class="right">
                <a class="close-popup" data-popup=".batchZipsUploadPopup">
                    <svg class="icon"><use xlink:href="#fa-closer"></use></svg>
                </a>
            </div>
        </div>
    </div>
    <div class="card zipsBox">
        <div class="card-header">
            <span>服务器上传</span>
            <a class="button-fill button color-orange" target="_blank" href="<?=Lev::toReRoute(['superman/settings', 'classify'=>'servermud', 'iden'=>'levmodules', 'id'=>'levs'])?>">
                <svg class="icon"><use xlink:href="#fa-set"></use></svg>
                服务器配置
            </a>
        </div>
        <div class="card-content-inner">
            <div class="item-input">
                <select class="batchZipsUploadServer" style="max-width: 100%">
                    <option value="">至少选择一个服务器</option>
                <?php foreach ($servers as $v):?>
                    <option value="<?=$v['id']?>" title="<?=$v['serverDomain']?>">上传到【<?=$v['descs'],$v['serverDomain']?>】</option>
                <?php endforeach;?>
                    <option value="levstore">上传到Lev模块商城【<?=\modules\levmodules\helpers\levmodulesSetHelper::storeApiDomain()?>】</option>
                </select>
            </div>
        </div>
        <div class="card-footer">
            <div class="flex-box">
                <a class="button button-fill color-orange batchZipsUploadBtn scale8">开始上传</a>
                <a class="button button-fill color-lightblue batchZipsBtn scale8">
                    <svg class="icon"><use xlink:href="#fa-txb"></use></svg>
                    批量压缩
                </a>
                <a class="button button-fill color-red batchZipsAndUploadBtn scale8">压缩并上传</a>
            </div>
            <a class="button button-fill color-blackg toServerLink scale8" target="_blank" _bk="1">访问上传服务器</a>
        </div>
    </div>
    <div class="mergeZipBox card">
        <div class="card-header">
            <span>合并压缩配置</span>
            <tips>【提示】请先执行压缩再合并</tips>
        </div>
        <div class="card-content-inner flex-box">
            <input type="text" name="mainIden" class="mainIden wd80" placeholder="主模块iden，例：levs">
            <select name="setMainIden" class="setMainIden wd40"></select>
            <select name="checkHideT" class="wd120 checkHideT">
                <option value="0">不关闭</option>
                <option value="1">关闭levs主模块前台显示及后台设置功能</option>
                <option value="2">压缩测试版（不删除安装文件）</option>
            </select>
            <input type="button" class="button button-fill scale8 wd50 wdmin mergeZipsBtn" value="执行">
            <div style="height:20px">
                <p><tips>* 将多个模块压缩到一起，实现一键安装。</tips></p>
                <p><tips>* 如需兼容DZ请在主模块编辑处创建DZ安装文件</tips></p>
            </div>
        </div>
    </div>
    <div class="mergeZipBox card">
        <div class="card-header">
            <span>管理配置</span>
            <tips>合并压缩时设置admin.php文件默认进入界面</tips>
        </div>
        <div class="card-content-inner flex-box changeAdminEnterInputBox">
            <input type="text" name="id" class="changeAdminEnterInput wd100" placeholder="管理iden，例：levs">
            <input type="text" name="r" class="changeAdminEnterInput wd220" placeholder="管理route，例：superman/settings">
            <input type="text" name="classify" class="changeAdminEnterInput wd100" placeholder="设置类别，例：1">
        </div>
    </div>
    <div class="tipsBB scale9" style="transform-origin: top;">
        <tips></tips>
    </div>
</div>
</div>
<script>
jQuery(function () {
    var isMergeid = <?=$js?1:0?>;
    var tips = '';
    var idsTitle = {};

    Levme.onOnce('popup:opened', '.batchZipsUploadPopup', function () {
        <?=$funcJs?>
    });
    Levme.onClick('.toServerLink', function () {
        var href = jQuery('.batchZipsUploadServer').find('option:selected').attr('title');
        if (!href) {
            myApp.alert('请先选择一个服务器');
            return;
        }
        jQuery(this).attr('href', href +'/levs.php?id=levs&r=admin-modules');
    });
    Levme.onClick('.batchZipsUploadPopupBtn', function () {
        myApp.popup('.batchZipsUploadPopup');
    });

    Levme.onClick('.mergeZipsPopupBtn', function () {
        myApp.popup('.batchZipsUploadPopup');
    });

    Levme.onClick('.mergeZipsBtn', function () {
        var ids = getCheckedIds();
        if (ids) {
            myApp.confirm('您确定要执行合并压缩吗？', function () {
                var mainIden = jQuery('input[name="mainIden"]').val();
                var checkHideT = jQuery('select[name="checkHideT"]').val();
                if (!mainIden) {
                    levtoast('请设置主模块iden');
                    return;
                }
                mergeZips(ids, mainIden, checkHideT, 1);
            });
        }
    });

    function mergeZips(ids, mainIden, checkHideT, unzip) {
        var changeAdminEnter = {};
        jQuery('.changeAdminEnterInput').each(function () {
            changeAdminEnter[jQuery(this).attr('name')] = jQuery(this).val();
        });

        unzip = unzip || 0;//1：解压，0：执行压缩
        showIconLoader(true);
        Levme.ajaxv.postv('<?=Lev::toReRoute(['admin-modules/merge-zip', 'id'=>'levmodules'])?>', function (data, status) {
            tips = data.message + '<br>' + tips;
            if (data.unzip) {
                tips = '解压完成，正在执行压缩<br>' +tips;
                mergeZips(ids, mainIden, checkHideT, 0);
            }
            showTips();
        }, {ids:ids,mainIden:mainIden,checkHideT:checkHideT, unzip:unzip, changeAdminEnter:changeAdminEnter});
    }

    Levme.onClick('.batchZipsBtn', function () {
        var ids = getCheckedIds();
        if (ids) {
            myApp.confirm('您确定要执行批量压缩吗？', function () {
                tips = '';
                myApp.popup('.batchZipsUploadPopup');
                loopAjaxBatchZips(ids);
            });
        }
    });
    Levme.onClick('.batchZipsUploadBtn', function () {
        var serverid = jQuery('.batchZipsUploadServer').val();
        if (!serverid) {
            myApp.alert('请选择上传服务器');
            Levme.animated('.batchZipsUploadServer');
            return;
        }
        var ids = getCheckedIds();
        if (ids) {
            myApp.confirm('您确定要执行批量【上传】吗？', function () {
                tips = '';
                loopAjaxBatchZipsUpload(ids, serverid);
            });
        }
    });
    Levme.onClick('.batchZipsAndUploadBtn', function () {
        var serverid = jQuery('.batchZipsUploadServer').val();
        if (!serverid) {
            myApp.alert('请选择上传服务器');
            Levme.animated('.batchZipsUploadServer');
            return;
        }
        var ids = getCheckedIds();
        if (ids) {
            myApp.confirm('您确定要执行批量【压缩并上传】吗？', function () {
                tips = '';
                jQuery('.tipsBB').html('');
                loopAjaxBatchZips(ids, serverid);
            });
        }
    });

    function loopAjaxBatchZips(ids, nextUploadserverid) {
        if (ids.length < 1) {
            tips = '压缩完成：<a class="inblk scale6 button button-fill vera" href="">刷新</a><br><br>' + tips;
            //Levme.showNotices(tips, '', 1000000);
            showTips();
            if (nextUploadserverid) {
                //tips = '';
                loopAjaxBatchZipsUpload(getCheckedIds(), nextUploadserverid);
            }
            return;
        }
        var zipid = ids.shift();
        showIconLoader(true);
        var _tips = '<tips>正在压缩：'+ zipid + idsTitle[zipid] + '</tips> -> ';
        showTips();
        Levme.ajaxv.getv('<?=Lev::toReRoute(['admin-modules/ajax-batch-zip', 'id'=>'levmodules'])?>', function (data, status) {
            hideIconLoader();
            var acmsg = data.message ? data.message : (status >0 ? '成功' : '失败');
            tips = _tips + '<p class="font12 scale9 inblk">执行结果：'+ acmsg + '</p><br>' + tips;
            showTips();

            loopAjaxBatchZips(ids, nextUploadserverid);
        }, {zipid:zipid});
    }

    function loopAjaxBatchZipsUpload(ids, serverid) {
        if (!ids) {
            myApp.alert('您至少选择一个压缩对象');
            return;
        }
        if (ids.length < 1) {
            tips = '上传完成：<a class="inblk scale6 button button-fill vera" href="">刷新</a><br><br>' + tips;
            //Levme.showNotices(tips, '', 1000000);
            //jQuery('.tipsBB').append(tips);
            showTips();
            return;
        }
        var zipid = ids.shift();
        showIconLoader(true);
        var _tips = '<tips>正在上传：'+ zipid + idsTitle[zipid] + '</tips> -> ';
        showTips();
        Levme.ajaxv.getv('<?=Lev::toReRoute(['admin-modules/ajax-batch-zip-upload', 'id'=>'levmodules'])?>', function (data, status) {
            hideIconLoader();

            var acmsg = data.message ? data.message : (status >0 ? '成功' : '失败');
            tips = _tips + '<p class="font12 scale9 inblk">执行结果：'+ acmsg + '</p><br>' + tips;
            showTips();

            loopAjaxBatchZipsUpload(ids, serverid);
        }, {zipid:zipid, serverid:serverid});
    }

    Levme.onOnce('change', '.setMainIden', function () {
        this.value &&
        jQuery('.mainIden').val(this.value);
    });

    function getCheckedIds() {
        var ids = [];
        jQuery('.setMainIden').html('<option value="">请选择</option>');
        jQuery('input[name="ids[]"]:checked').each(function () {
            var valx = this.value;

            ids.push(valx);
            idsTitle[valx] = jQuery(this).parents('tr').find('.column-name input').val();

            var iden = jQuery.trim(jQuery(this).parents('tr').find('.column-identifier a').text());
            jQuery('.setMainIden').append('<option value="'+iden+'">'+ iden +'</option>');
        });
        if (ids.length < 1) {
            myApp.alert('抱歉，您至少选择一项', function () {
                Levme.animated('input[name="ids[]"]');
            });
            Levme.animated('input[name="ids[]"]');
            return false;
        }
        return ids;
    }

    function showTips(tip) {
        jQuery('.tipsBB').html(tip ? tip : tips);
        //Levme.showNotices(tips, '', 1000000);
    }

    isMergeid && getCheckedIds();
});
</script>
